<?php namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

/**
 * 奖项
 * Class Award
 *
 * @package App\Models
 * @property int $id
 * @property string $name 名称
 * @property string $code 代码
 * @property string $proposition_companies 对应命题企业
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\PropositionCompany[] $propositionCompanies
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereCode($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award wherePropositionCompanies($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereDeletedAt($value)
 * @property \Carbon\Carbon $began_at
 * @property \Carbon\Carbon $ended_at
 * @property \Carbon\Carbon $modified_before
 * @property boolean $is_hidden
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereBeganAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereEndedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereModifiedBefore($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Award whereIsHidden($value)
 * @mixin \Eloquent
 */
class Award extends Model
{
    use SoftDeletes;

    protected $table = 'awards';

    protected $dates = ['deleted_at', 'began_at', 'ended_at', 'modified_before'];

    protected $fillable = ['name', 'code', 'proposition_companies', 'began_at', 'ended_at', 'modified_before', 'is_hidden', 'print_text','entryFormTitle'];

    /**
     * 用于select的列表
     * @return array
     */
    public static function getListData($all = false)
    {
        if ($all) {
            $infos = self::whereIsHidden(0)->get();
        } else {
            $infos = self::getAvailableLists();
        }
        $listData = ['0' => '请选择'];
        foreach ($infos as $info) {
            $listData[$info->id] = $info->name;
        }
        return $listData;
    }

    /**
     * 对应的命题企业
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function propositionCompanies()
    {
        return $this->belongsToMany('\App\Models\PropositionCompany', 'award_company', 'award_id', 'company_id')
            ->withPivot('sort', 'type' , 'start_at' , 'end_at', 'id')
            ->orderBy('sort', 'asc');
    }




    /**
     * 获取可用的数据
     * @return mixed
     */
    public static function getAvailableLists()
    {
        $ct = Carbon::now();
        $infos = self::where('began_at', '<', $ct)
            ->where('ended_at', '>', $ct)
            ->get();
        return $infos;
    }

    /**
     * 检查该奖项是否可以上传作品
     * @return bool
     */
    public function worksCouldCreate()
    {
        $ct = Carbon::now();
        if ($ct->gte($this->began_at) && $ct->lte($this->ended_at)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查该奖项是否可以修改作品
     * @return bool
     */
    public function worksCouldEdit()
    {
        $ct = Carbon::now();
        if ($this->modified_before && $ct->gt($this->modified_before)) {
            return false;
        } else {
            return true;
        }
    }

}